<?php
/*
 * EmailComponent for CakePHP
 *
 * @author      gwoo <gwoo@rd11.com>
 * @version     0.10.8.2176
 * @license		OPPL
 *
 */
class ImageComponent extends Object
{

	var $params;
	var $rdImage;
	var $prop;
	var $user_name ='kamome';
	var $error = array();


	//set some image properties
//	var $crop = array('square','scale');
    var $crop = array('set','set');
	var $dir_confirm = array('/file/confirm','/file/confirm/thumbs');
	var $dir = array('/file','/file/thumbs');

	var $filename = array();


	
	function __construct(){
		$this->rdImage = new rdImageComponent();
		$this->prop = new PropComponent();
		$dir_confirm = $this->prop->getValue('config.image.dir_confirm');
		$dir = $this->prop->getValue('config.image.dir');
		$this->dir_confirm = array($dir_confirm.DS.session_id(),$dir_confirm.DS.session_id().DS.'thumbs'); 
		$this->dir = array($dir,$dir.DS.'thumbs'); 
		$this->rdImage->jpegquality = '100';
		$this->rdImage->resizeWidth = array('120','70');
		$this->rdImage->resizeHeight = array('120','70');
	}
	

	function saveConfirmPhoto($form,$field)
	{
		$image = $form[$field];
		uses('folder');
		if(empty($image['tmp_name'][0]))
		{
			$this->errors[] = 'error #1001: you did not select a file';
			return false;
		}

		if(is_array($this->dir_confirm))
		{
			foreach($this->dir_confirm as $dir_confirm)
			{
				if(!new Folder(WWW_ROOT.$dir_confirm, true, 0777))
				{
					$this->errors[] = 'error #1002: the photo '.$dir_confirm.' directory was not created.';
					return false;
				}
				else{
					chown(WWW_ROOT.$dir_confirm,$this->user_name);
				}
			}
		}
		
		/*Not using right now cause i like using the primary key for the image name
		if($this->saveAs)
		{
			$this->rdImage->filename = $this->saveAs[0];
			array_shift($this->saveAs);
		}
		$this->rdImage->filename = (isset($this->rdImage->filename)) ? $this->rdImage->filename : $image['name'][$key]; 					
		*/
		
		$i ='0';
		foreach($image['name'] as $key=>$value)
		{
			if($image['tmp_name'][$key])
			{		
				$this->rdImage->loadtmpfile($image['tmp_name'][$key]);
				
				$this->rdImage->filename = $key+1;

				$imageName = $field . '_' . $this->rdImage->filename . rand(100,999) . $this->rdImage->extension; 

				//setting this so it will reset to tmp for multiple files
				$this->rdImage->reset = 1;
				//if width is greater	
				if($this->rdImage->orientation == 'landscape')
				{  
					$n = 0;
					foreach($this->rdImage->resizeWidth as $width)
					{
						$this->rdImage->dir = $this->dir_confirm[$n];
						if($width <= '100' && $this->crop[$n] == 'scale')
						{
							$this->rdImage->resizetoheight($width);
						}
						elseif($this->crop[$n] == 'square')
						{
							$this->rdImage->crop($width, $width);
						}
						elseif($this->crop[$n] == 'set'){
							$size=$this->rdImage->_width/$width;
							$_height=$this->rdImage->_height/$size;
							$this->rdImage->crop($width, $_height);
						}
						else
						{
							$this->rdImage->resizetowidth($width);
						}
						$this->rdImage->savefile($imageName);
						$n++;	
					}
				}
				//if height is greater
				else
				{
					$n = 0;
					foreach($this->rdImage->resizeHeight as $height)
					{
						$this->rdImage->dir = $this->dir_confirm[$n];
						if($this->crop[$n] == 'scale')
						{
							$this->rdImage->resizetoheight($height);
						}
						elseif($this->crop[$n] == 'square')
						{
							$this->rdImage->crop($height, $height);
						}
						elseif($this->crop[$n] == 'set'){
							$size=$this->rdImage->_height/$height;
							$_width=$this->rdImage->_width/$size;
							
							$this->rdImage->crop($_width, $height);
						}
						
						$this->rdImage->savefile($imageName);
						$n++;
					}
				}
				if(!is_file($this->rdImage->ImagePath))
				{
					$this->errors[] = 'error #1003: the photo was not saved.';
					return false;
				}
				else
				{
					$this->filename[$key] = $imageName;
				}
			}
			$i++;							   
		}//end image foreach
		return true;
	}

	function savePhoto($path,$filename){
		uses('folder');
		$dir_confirm = $this->dir_confirm;

		foreach($this->dir as $key => $dir){
			if(!new Folder(WWW_ROOT.$dir.DS.$path, true, 0777))
			{
				$this->errors[] = 'error #1002: the photo '.$this->dir.DS.$path.DS.$filename.' directory was not created.';
				return false;
			}
			else{
				chown(WWW_ROOT.$dir.DS.$path,$this->user_name);
			}

			if (file_exists(WWW_ROOT.$dir_confirm[$key].DS.$filename)){
				copy(WWW_ROOT.$dir_confirm[$key].DS.$filename, WWW_ROOT.$dir.DS.$path.DS.$filename) 
				or die ("ファイルのコピーに失敗しました : From : ".WWW_ROOT.$dir_confirm[$key].DS.$filename." : To : ".WWW_ROOT.$dir.DS.$path.DS.$filename); 
			}
		}
	}

	function saveAllPhoto($path){
		uses('folder');
		$dir_confirm = $this->dir_confirm;

		$filename = array();
		if ($handle = opendir(WWW_ROOT.$dir_confirm)) {
		   while (false !== ($file = readdir($handle))) {
		   		if ($file != "." && $file != ".." && is_file(WWW_ROOT.$dir_confirm.DS.$file)) {
			       	$filename[] = $file;
		   		}
		   }
		   closedir($handle);
		}
		$this->filename = $filename;
		
		foreach($this->dir as $key => $dir){
			foreach ($this->filename as $filename){
				if(!new Folder(WWW_ROOT.$dir.DS.$path, true, 0777))
				{
					$this->errors[] = 'error #1002: the photo '.$this->dir.DS.$path.' directory was not created.';
					return false;
				}
				else{
					chown(WWW_ROOT.$dir.DS.$path,$this->user_name);
				}
			
				if (file_exists(WWW_ROOT.$dir_confirm.DS.$filename)){
					copy(WWW_ROOT.$dir_confirm.DS.$filename, WWW_ROOT.$dir.DS.$path.DS.$filename) 
					or die ("ファイルのコピーに失敗しました : From : ".WWW_ROOT.$dir_confirm[$key].DS.$filename." : To : ".WWW_ROOT.$dir.DS.$path.DS.$filename); 
				}
			}
		}
	}
	
		

}

?>